#include <ESP8266WiFi.h>
#include <ESP8266_Seniverse.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <WiFiManager.h>

#include <dht11.h>//引入DHT11库
dht11 DHT11;//定义传感器类型
#define DHT11PIN D5//定义传感器连接引脚

#include <U8g2lib.h>
#define rst  -1
U8G2_SSD1306_128X64_NONAME_F_HW_I2C  u8g2(U8G2_R0 , rst);

const unsigned char mi[] U8X8_PROGMEM = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0xF0, 0xFF, 0xFF, 0x1F, 0x80, 0x3F, 0xFC, 0xF0, 0xFF, 0xFF, 0x7F, 0x80,
  0x3F, 0xFC, 0xF0, 0xFF, 0xFF, 0xFF, 0x80, 0x3F, 0xFC, 0xF0, 0xFF, 0xFF, 0xFF, 0x81, 0x3F, 0xFC, 0xF0, 0xFF, 0xFF, 0xFF, 0x81, 0x3F, 0xFC, 0xF0, 0xFF, 0xFF, 0xFF, 0x83, 0x3F, 0xFC, 0xF0, 0x07,
  0x00, 0xFC, 0x83, 0x3F, 0xFC, 0xF0, 0x07, 0x00, 0xF8, 0x83, 0x3F, 0xFC, 0xF0, 0x07, 0x00, 0xF8, 0x83, 0x3F, 0xFC, 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x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC
};
// weather
const String weathers[] = {"晴", "晴", "晴", "晴", "多云", "晴间多云", "晴间多云", "大部多云", "大部多云", "阴",
                           "阵雨", "雷阵雨", "雷雨冰雹", "小雨", "中雨", "大雨", "暴雨", "大暴雨", "特大暴雨",
                           "冻雨", "雨夹雪", "阵雪", "小雪", "中雪", "大雪", "暴雪", "浮尘", "扬沙", "沙尘暴",
                           "强沙暴", "雾", "霾", "风", "大风", "飓风", "热带风暴", "龙卷风", "冷", "热", "未知"
                          };


String reqUserKey = "Sw72sh040w5ibUetL";// 你的心知天气私钥
String reqLocation = "taiyuan";// 你的城市（拼音）
String reqUnit = "c";// 摄氏/华氏

WeatherNow weatherNow;
Forecast forecast;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "ntp1.aliyun.com", 60 * 60 * 8, 30 * 60 * 1000);

int convert_flag = 0;

void setup() {
  Serial.begin(115200);

  //启动页
  u8g2.begin();
  u8g2.enableUTF8Print();
  u8g2.clearBuffer();
  u8g2.drawXBMP( 39 , 0 , 50 , 50, mi );
  u8g2.setFont(u8g2_font_wqy12_t_gb2312);
  u8g2.setCursor(16, 60);
  u8g2.print("桌面天气时钟beta");
  u8g2.sendBuffer();
  delay(2000);

  connectWiFi();// 连接wifi
  // 配置心知天气请求信息
  weatherNow.config(reqUserKey, reqLocation, reqUnit);
  forecast.config(reqUserKey, reqLocation, reqUnit);
  timeClient.begin();
}

void loop() {
  u8g2.clearBuffer();// 清空显示设备内部缓冲区

  
  switch (convert_flag)
  {
  case 0:Time_display();
    break;
  case 1:Today_display();
    break;
  case 2:Forecast_display();
    break;
  case 3:DHT_display();
    break;
  
  }

  u8g2.sendBuffer();// 显示缓冲区内容
  delay(2000);
}

// 连接WiFi
void connectWiFi() {
  // 建立WiFiManager对象
  WiFiManager wifiManager;
  // 自动连接WiFi。以下语句的参数是连接ESP8266时的WiFi名称
  wifiManager.autoConnect("AutoConnectAP");
}

// 今天天气实况
void Today_display(){

  if (weatherNow.update() && forecast.update()) { // 更新天气信息

    u8g2.setFont(u8g2_font_wqy16_t_gb2312b);
    u8g2.setCursor(20, 18);
    u8g2.print(weathers[weatherNow.getWeatherCode()]);
    
    
    u8g2.setFont(u8g2_font_wqy12_t_gb2312b);
    u8g2.setCursor(36, 48);
    String str1 = weathers[forecast.getDayCode(0)] + " " + forecast.getLow(0) + "-" + forecast.getHigh(0) + "℃";
    u8g2.print(str1);
    
    //室内温湿度
    DHT11.read(DHT11PIN); //更新传感器所有信息
    u8g2.setFont(u8g2_font_wqy12_t_gb2312b);
    u8g2.setCursor(64, 12);
    u8g2.print("湿度：");
    u8g2.print((float)DHT11.humidity,2);
    u8g2.setCursor(64, 24);
    u8g2.print("温度：");
    u8g2.print((float)DHT11.temperature,2);
  }
  convert_flag = 2;
}

// 明后天预报 
void Forecast_display(){
  u8g2.setFont(u8g2_font_wqy12_t_gb2312);

  //天气
  if (weatherNow.update() && forecast.update()) { // 更新天气信息

    //今天的天气、温度
    u8g2.setCursor(43, 12);
    String str1 = weathers[weatherNow.getWeatherCode()] + " " + weatherNow.getDegree() + "℃(今天)";
    u8g2.print(str1);

    //明天的天气、温度范围
    u8g2.setCursor(43, 27);
    str1 = weathers[forecast.getDayCode(1)] + " " + forecast.getLow(1) + "-" + forecast.getHigh(1) + "℃";
    u8g2.print(str1);

    //后天的天气、温度范围
    u8g2.setCursor(43, 42);
    str1 = weathers[forecast.getDayCode(2)] + " " + forecast.getLow(2) + "-" + forecast.getHigh(2) + "℃";
    u8g2.print(str1);
  }
  convert_flag = 0;

}

// 时间显示
void Time_display(){

  u8g2.setFont(u8g2_font_wqy16_t_gb2312b);
  if (timeClient.update()) {
    u8g2.setCursor(46, 32);
    String time = timeClient.getFormattedTime().substring(0, 5);
    u8g2.print(time);
  } else { 
    u8g2.setCursor(53, 61);
    u8g2.print(">_<...网络慢");
  }
  convert_flag = 1;
}

// 室内温湿度显示
void DHT_display(){
 
}
